package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private Executor threadPoolTaskExecutor;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询专辑详情信息
     *
     * @param albumId
     * @return {"announcer":{},"albumInfo":{},......}
     */
    @Override
    public Map<String, Object> getItemData(Long albumId) {
        //0.为了避免缓存穿透问题，判断用户访问的专辑id在布隆过滤器中是否存在。
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean contains = bloomFilter.contains(albumId);
        if (!contains) {
            throw new GuiguException(404, "专辑不存在");
        }
        //1.创建Map集合用于封装详情页所需参数
        //问题：hashMap线程不安全
        Map<String, Object> map = new ConcurrentHashMap<>();

        //2.远程调用专辑服务获取专辑详情信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolTaskExecutor);

        //3.远程调用专辑服务，根据专辑所属分类id查询分类信息
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Long category3Id = albumInfo.getCategory3Id();
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(category3Id).getData();
            Assert.notNull(baseCategoryView, "专辑{}所属分类{}不存在", albumId, category3Id);
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolTaskExecutor);


        //4.远程调用专辑服务，根据主播id查询主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Long userId = albumInfo.getUserId();
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
            Assert.notNull(userInfoVo, "专辑{}所属主播{}不存在", albumId, userId);
            map.put("announcer", userInfoVo);
        }, threadPoolTaskExecutor);


        //5.远程调用专辑服务获取专辑统计信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑{}统计信息不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolTaskExecutor);

        //6.封装结果返回
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                baseCategoryViewCompletableFuture,
                announcerCompletableFuture,
                albumStatVoCompletableFuture
        ).join();

        return map;

    }

}
